<?php

namespace app\api\controller\exam;

use app\common\controller\Api;

use think\Db;

/**
 * 首页接口
 */
class Papers extends Api
{
//    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\exam\Papers;
        $this->questionmodel = new \app\admin\model\exam\QuestionModel;


    }

    /**
     * 作业列表
     *
     */
    public function index()
    {

        $userinfo = $this->auth->getUserinfo();
        $sort = 'id';
        $order = 'DESC';
        $offset = 1;
        $limit = 10;
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');


        $where['exp'] = Db::raw("FIND_IN_SET({$profile_id},share_ids)");

        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $rows = $list->toArray()['data'];
        foreach ($rows as &$row) {
            $row['realname'] = Db::name('classes_teacher')->where('profile_id', $row['profile_id'])->value('realname');
            $row['queuecount'] = count(explode(',', $row['question_ids']));
            $row['createtime_formt'] = date("Y-m-d", $row['createtime']);
        }

        $result = array("total" => $list->total(), "rows" => $rows);

        return json($result);
    }

    public function info()
    {
        $id = $this->request->post('id/d', 0);
        $userinfo = $this->auth->getUserinfo();
        $sort = 'id';
        $order = 'DESC';
        $offset = 1;
        $limit = 10;
        $profile_id = Db::name('classes_teacher')->where('user_id', $userinfo['user_id'])->value('profile_id');
        if ($id <= 0) {
            return $this->error('参数错误');
        }
        $where['id']=$id;
        $where['exp'] = Db::raw("FIND_IN_SET({$profile_id},share_ids)");
        if(null==$data=$this->model->where($where)->find()){
            return $this->error('不存在');
        }
        $queueids=explode(',',$data['question_ids']);
        $queueilist=[];
        foreach ($queueids as $key=>$value){
            $queueilist[$key]=$this->get_view($value);
        }
        $data['queueilist']=$queueilist;
        return $this->success($data);

    }
    protected function get_view($qid)
    {
        $row = $this->questionmodel->get($qid);

        $data = array();
        switch ($row['kind']) {
            case 'SINGLE':
                $data['kind'] = '单选题';
                break;
            case 'JUDGE':
                $data['kind'] = '判断题';
                break;
            case 'MULTI':
                $data['kind'] = '多选题';
                break;
            case 'FILL':
                $data['kind'] = '填空题';
                break;
            default:
                $data['kind'] = '单选题';
        }
        switch ($row['difficulty']) {
            case 'EASY':
                $data['difficulty'] = '简单';
                break;
            case 'GENERAL':
                $data['difficulty'] = '普通';
                break;
            case 'MULTI':
                $data['HARD'] = '困难';
                break;
            default:
                $data['difficulty'] = '单选题';
        }
        $data['describe'] = $row['describe'];
        $data['title'] = $row['title'];
        $data['answer'] = $row['answer'];
        $data['id'] = $row['id'];
        $data['other_url'] = $row['other_url'];
        $data['other_name'] = $row['other_name'];

        $options_json = json_decode($row['options_json'], true);
        $data['options_json'] = $options_json;


        return $data;

    }

}
